/*
 *  Copyright (C) 2010 takaji
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package examples;

import dakside.csv.CSVFile;
import dakside.csv.CSVFileReader;
import dakside.csv.CSVFileWriter;

/**
 * Example 3: Clone CSV file
 * @author takaji
 */
public class Example3 {

    /**
     * Test clone CSV file<br/>
     * Step 1: read CSV data into memory (CSVFile object) <br/>
     * Step 2: write CSVFile object to a file
     * @param filename
     */
    public static void testCloneCSV(String filename) {
        // try to parse the whole file to in-memory object
        CSVFile csvData = CSVFileReader.parseFile(filename);
        // clone CSV
        //create a new csv writer
        CSVFileWriter writer = new CSVFileWriter(filename + "_new.csv");
        // write csvData to a new file
        writer.writeFile(csvData);
        // when close -> auto flush
        writer.close();
    }

    /**
     * @param args  the command line arguments
     */
    public static void main(String[] args) {
        // first argument is file name
        // we should have 1 argument here
        if (args.length != 1) {
            System.out.println("Usage:");
            System.out.println("java -jar dakside.csv.jar /path/to/file.csv");
            return;
        }
        String filename = args[0];
        testCloneCSV(filename);

        System.out.println("Finished.");
    }
}
